import { requestClient } from '#/api/request';

export namespace RechargeApi {
  /** 充值列表查询参数 */
  export interface RechargeListParams {
    page?: number;
    per_page?: number;
    recharge_id?: string;
    status?: 'recharging' | 'recharge_failed' | 'arrived' | 'settled' | 'partial_refund' | 'refunded';
    landing_company?: string;
    start_date?: string;
    end_date?: string;
    sort_by?: string;
    sort_order?: 'asc' | 'desc';
  }

  /** 落地公司选项 */
  export interface LandingCompanyOption {
    value: string | number;
    label: string;
    name?: string;
    code?: string;
    billing_categories?: string[];
  }

  /** 充值记录接口 */
  export interface RechargeRecord {
    id: string;
    recharge_id: string;
    recharge_number: string;
    tenant_id: number;
    landing_company: string;
    amount: number;
    bank_name: string;
    bank_account: string;
    status: 'recharging' | 'recharge_failed' | 'arrived' | 'settled' | 'partial_refund' | 'refunded';
    status_display: string;
    status_description?: string;
    remarks?: string;
    created_at: string;
    updated_at: string;
    arrived_at?: string;
    settled_at?: string;
    refunded_at?: string;
    created_by?: number;
    updated_by?: number;
  }

  /** 创建充值参数 */
  export interface CreateRechargeParams {
    landing_company: string;
    amount: number;
    bank_name: string;
    bank_account: string;
    remarks?: string;
  }

  /** 分页响应结构 */
  export interface RechargeListResponse {
    data: RechargeRecord[];
    current_page: number;
    per_page: number;
    total: number;
    last_page: number;
    from: number | null;
    to: number | null;
    first_page_url: string | null;
    last_page_url: string | null;
    next_page_url: string | null;
    prev_page_url: string | null;
    path: string | null;
    links: Array<{
      url: string | null;
      label: string;
      active: boolean;
    }>;
  }

  /** API响应包装 */
  export interface ApiResponse<T = any> {
    success: boolean;
    data: T;
    message?: string;
    error?: any;
  }
}

/**
 * 获取充值列表
 * @param params 查询参数
 * @returns 充值列表响应数据
 */
export async function getRechargeListApi(params: RechargeApi.RechargeListParams) {
  return requestClient.get<RechargeApi.RechargeListResponse>('/api/tenant/recharges', {
    params,
  });
}

/**
 * 创建充值记录
 * @param data 创建充值参数
 * @returns 创建结果
 */
export async function createRechargeApi(data: RechargeApi.CreateRechargeParams) {
  return requestClient.post<RechargeApi.RechargeRecord>('/api/tenant/recharges', data);
}

/**
 * 获取落地公司选项
 */
export async function getLandingCompanyOptionsApi() {
  return requestClient.get<RechargeApi.LandingCompanyOption[]>('/api/tenant/landing-companies/options');
}

/**
 * 获取充值详情
 */
export async function getRechargeDetailApi(id: string) {
  return requestClient.get<RechargeApi.ApiResponse<RechargeApi.RechargeRecord>>(`/api/tenant/recharges/${id}`);
}

/**
 * 导出充值数据
 */
export async function exportRechargeDataApi(params: RechargeApi.RechargeListParams) {
  return requestClient.get<RechargeApi.ApiResponse<{ count: number; export_url: string }>>('/api/tenant/recharges/export', { params });
}